package jiubeirobot.com.robotshop.ui.activity.manange;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import jiubeirobot.com.robotshop.BuildConfig;
import jiubeirobot.com.robotshop.R;
import jiubeirobot.com.robotshop.base.Activitys;
import jiubeirobot.com.robotshop.base.BaseActivity;
import jiubeirobot.com.robotshop.base.BaseBean;
import jiubeirobot.com.robotshop.bean.LoginManageBean;
import jiubeirobot.com.robotshop.bean.ObjBean;
import jiubeirobot.com.robotshop.data.net.APIManager;
import jiubeirobot.com.robotshop.data.net.Flags;
import jiubeirobot.com.robotshop.utils.BasicTool;
import jiubeirobot.com.robotshop.utils.DataUtil;
import jiubeirobot.com.robotshop.utils.RegexUtils;
import jiubeirobot.com.robotshop.utils.SharedPreferencesUtils;
import jiubeirobot.com.robotshop.utils.TS;

/**
 * @author TheJoker丶豪
 *         登录系统设置管理的activity
 * @date 创建时间:2017/8/21
 */

public class LoginManagerActivity extends BaseActivity {
    @BindView(R.id.et_manage_account)
    EditText etaccount;
    @BindView(R.id.et_manage_password)
    EditText etpassword;
    @BindView(R.id.btn_manage_login)
    Button btnlogin;
    @BindView(R.id.tv_manage_forget)
    TextView tvforget;
    @BindView(R.id.llyt_back)
    LinearLayout llyt_back;
    @BindView(R.id.tv_title)
    TextView tvtitle;
    @BindView(R.id.iv_search_icon)
    ImageView ivsearch;

    private String username;

    @Override
    public int pageLayout() {
        return R.layout.activity_login_manager;
    }

    @Override
    public void initTitle() {
        ivsearch.setVisibility(View.GONE);
        tvtitle.setText("登 录");
        etaccount.clearFocus();
        etpassword.requestFocus();
        username = DataUtil.getInstance().getMobileNumber(LoginManagerActivity.this);
        if (!TextUtils.isEmpty(username)) {
            etaccount.setText(username);
        }
    }

    @Override
    public void initView() {
        if (BuildConfig.DEBUG){
            etpassword.setText("123456");
        }

    }

    @Override
    public void onSuccess(BaseBean bean, int flag) {
        switch (flag) {
            case Flags.FLAG_LOGIN_SYSTEM:
                //根据返回信息判断是否应该登录成功
                BaseBean<ObjBean<LoginManageBean>> loginbean = bean;
                LoginManageBean manageBean = loginbean.getData().getObj();
                SharedPreferencesUtils.putString(LoginManagerActivity.this,"verify",manageBean.getVerify());
                Activitys.toSetSys(LoginManagerActivity.this);
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    protected boolean isOpenStatus() {
        return super.isOpenStatus();
    }

    @OnClick({R.id.btn_manage_login, R.id.llyt_back})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.btn_manage_login:
                String password = etpassword.getText().toString().trim();
                if(checklogin(username,password)){
                    APIManager.getInstance().loginSys(this, username, password);
                }
                break;
            case R.id.llyt_back:
                finish();
                break;
            default:
                break;
        }
    }
    /**
     * 描述      :  检验登录的格式
     * 方法名    :  checklogin
     * 修改备注
     */
    private boolean checklogin(String et_account, String et_password) {
        //在对输入的登录账号和密码的验证需要从 账号和密码的验证入手
        if (!BasicTool.isNotEmpty(et_account)) {
            TS.showToCenter(getString(R.string.cellar_manage_login_account_not_null));
            return false;
        }
        if (!RegexUtils.checkMobile(et_account)) {
            TS.showToCenter(getString(R.string.cellar_manage_login_account_fail));
            return false;
        }
        if (!BasicTool.isNotEmpty(et_password)) {
            TS.showToCenter(getString(R.string.cellar_manage_login_password_not_null));
            return false;
        }
        return true;
    }

}
